import request from '@/utils/axios'
import type { AxiosResponse } from 'axios'
import type { SsoUser, RegisterParams, ResponseData, QueryParams } from './type'

/**
 * 用于调用 API 接口的函数
 * @method SsoUserApi 获取用户相关
 */

export default {
    // 获取用户列表或单个用户信息
    getUsers(params?: QueryParams): Promise<AxiosResponse<ResponseData<SsoUser[]>>> {
      return request({
        url: '/api/ssoUser',
        method: 'get',
        params
      })
    },
  
    // 获取单个用户详情
    getUser(userId: string): Promise<AxiosResponse<ResponseData<SsoUser>>> {
      return request({
        url: `/api/ssoUser/${userId}`,
        method: 'get'
      })
    },
  
    // 创建单个用户
    createUser(data: SsoUser): Promise<AxiosResponse<ResponseData<SsoUser>>> {
      const formData = new FormData()
      Object.entries(data).forEach(([key, value]) => {
        if (value !== undefined) {
          formData.append(key, value.toString())
        }
      })
      return request({
        url: '/api/ssoUser',
        method: 'post',
        data: formData
      })
    },
  
    // 批量创建用户
    createUsers(userList: SsoUser[]): Promise<AxiosResponse<ResponseData<SsoUser>>> {
      const formData = new FormData()
      formData.append('SsoUserList', JSON.stringify(userList))
      return request({
        url: '/api/ssoUser',
        method: 'post',
        data: formData
      })
    },
  
    // 更新用户信息
    updateUser(userId: string, data: Partial<SsoUser>): Promise<AxiosResponse<ResponseData<SsoUser>>> {
      return request({
        url: `/api/ssoUser/${userId}`,
        method: 'put',
        data
      })
    },
  
    // 敏感信息更新
    sensitiveUpdate(userId: string, data: {
      login_type?: number
      user_type?: number
      status?: number
      is_delete?: number
    }): Promise<AxiosResponse<ResponseData<SsoUser>>> {
      const formData = new FormData()
      Object.entries(data).forEach(([key, value]) => {
        if (value !== undefined) {
          formData.append(key, value.toString())
        }
      })
      return request({
        url: `/api/ssoUser/update/${userId}`,
        method: 'put',
        data: formData
      })
    },
  
    // 删除用户
    deleteUser(userId: string): Promise<AxiosResponse<ResponseData<any>>> {
      return request({
        url: `/api/ssoUser/${userId}`,
        method: 'delete'
      })
    },
  
    // 用户注册
    register(data: RegisterParams): Promise<AxiosResponse<ResponseData<any>>> {
      return request({
        url: '/api/ssoUser/register',
        method: 'post',
        data
      })
    }
  }